/**
 * 
 */
package vo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

/**
 * @author Saulo
 * 
 */
public class MedidaDAO {

	// SALVAR MEDIDA
	public void salvarMedida(MedidaVO medida) {

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		session.saveOrUpdate(medida);

		transaction.commit();

	}

	// EXCLUIR MEDIDA
	public void excluir(Integer idMed) {
	Transaction transaction = null;

	MedidaVO medida = obter(idMed);
	
	Session session = HibernateUtil.currentSession();
	transaction = session.beginTransaction();

	session.delete(medida);

	transaction.commit();
}

	// OBTER MEDIDA
	public MedidaVO obter(Integer idMed) {
		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Criteria criteria = session.createCriteria(MedidaVO.class);
		criteria.add(Restrictions.eq("idMed", idMed));

		MedidaVO medida = (MedidaVO) criteria.uniqueResult();

		transaction.commit();

		return medida;

	}

	@SuppressWarnings("unchecked")
	public List<MedidaVO> consultarMedidas(MedidaVO medidaConsulta) {
		List<MedidaVO> listaDeMedidas = new ArrayList<MedidaVO>();

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Criteria criteria = session.createCriteria(MedidaVO.class);

		criteria.add(Restrictions.like("descriMed", medidaConsulta.getDescriMed()+ "%"));

		listaDeMedidas = criteria.list();

		transaction.commit();
		return listaDeMedidas;
	}

	@SuppressWarnings("unchecked")
	public List<MedidaVO> consultarTodasMedidas() {
		List<MedidaVO> listaDeMedidas = new ArrayList<MedidaVO>();

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Criteria criteria = session.createCriteria(MedidaVO.class);

		listaDeMedidas = criteria.list();

		transaction.commit();
		return listaDeMedidas;
	}
	
	public MedidaVO consultarMedidaPorID(int id, Session session) {
		MedidaVO medida = new MedidaVO();
		Criteria criteria = session.createCriteria(MedidaVO.class).add(Restrictions.eq("idMed", id));
		medida = (MedidaVO) criteria.uniqueResult();
		return medida;
	}
}
